﻿using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Polly.Wrap;

namespace DjangoRESTFrameworkDAL.REST
{
    public interface IGetList<T>
    {
        string Url { get; }
    }


    public static class GetListEx
    {
        /// <summary>
        ///     返回列表对象
        /// </summary>
        /// <typeparam name="T">序列化数据模型类型</typeparam>
        /// <param name="dal">DAL实例化对象</param>
        /// <returns>返回实例列表</returns>
        public static async Task<IEnumerable<T>> GetListAsync<T>(this IGetList<T> dal)
        {
            var response = await RestHttpClient.Instance.GetWithPolicyAsync(dal.Url);
            var responseBody = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<List<T>>(responseBody);
        }
    }
}